package com.ysd.lis.service.lab.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.lab.LabMaininfoXsGroup;
import com.ysd.lis.entity.lab.LabMaininfoXsReason;
import com.ysd.lis.mapper.lab.LabMaininfoXsGroupMapper;
import com.ysd.lis.mapper.lab.LabMaininfoXsReasonMapper;
import com.ysd.lis.request.AddAndUpdateCancleauditReason;
import com.ysd.lis.request.DeleteParam;
import com.ysd.lis.service.lab.LabMaininfoXsReasonService;
import com.ysd.util.IncrementalNumberGenerator;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 不合格原因内容表 服务实现类
 * </p>
 *
 * @author Bai
 * @since 2024-02-02
 */
@Service
public class LabMaininfoXsReasonServiceImpl extends ServiceImpl<LabMaininfoXsReasonMapper, LabMaininfoXsReason> implements LabMaininfoXsReasonService {


    @Autowired
    LabMaininfoXsReasonMapper labMaininfoXsReasonMapper;
    @Autowired
    LabMaininfoXsGroupMapper labMaininfoXsGroupMapper;
    @Override
    public Result getCancleauditReasonList(LabMaininfoXsReason labUnqualifiedReason) {
        MPJLambdaWrapper<LabMaininfoXsReason> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(LabMaininfoXsReason.class);
        queryWrapper.selectAs("lug", LabMaininfoXsGroup::getCancleauditName,LabMaininfoXsReason::getCancleauditName);
        queryWrapper.leftJoin(LabMaininfoXsGroup.class,"lug", LabMaininfoXsGroup::getId,LabMaininfoXsReason::getCancleauditGroupId);
        queryWrapper.eq(LabMaininfoXsReason::getDelFlag, 0);
        if(ToolsUtils.isNotEmpty(labUnqualifiedReason)){
            if(ToolsUtils.isNotEmpty(labUnqualifiedReason.getKeywords())){
                queryWrapper.like(LabMaininfoXsReason::getReason, labUnqualifiedReason.getKeywords());
            }
        }
        queryWrapper.eq(StrUtil.isNotEmpty(labUnqualifiedReason.getCancleauditGroupId()),LabMaininfoXsReason::getCancleauditGroupId,labUnqualifiedReason.getCancleauditGroupId());
        queryWrapper.orderByAsc(LabMaininfoXsReason::getSeq);
        List<LabMaininfoXsReason> list =  labMaininfoXsReasonMapper.selectList(queryWrapper);

        return Result.succ(list);
    }

    @Override
    public Result add(AddAndUpdateCancleauditReason addAndUpdateUnqualiReason) {
        if(ToolsUtils.isEmpty(addAndUpdateUnqualiReason)){
            return Result.fail("入参不可为空");
        }
        List<LabMaininfoXsReason> unqualifiedReasons = addAndUpdateUnqualiReason.getCancleauditReasons();
        SysUser sysUser = RedisUserManager.getUser();
        for (LabMaininfoXsReason unqualifiedGroup : unqualifiedReasons) {
            unqualifiedGroup.setOrgId(sysUser.getOrgId());
            unqualifiedGroup.setCode(IncrementalNumberGenerator.generateIncrementalNumber7());
        }
        boolean save = this.saveBatch(unqualifiedReasons);
        if(save){
            return Result.fail("新增成功");
        }else {
            return Result.fail("新增失败");
        }
    }

    @Override
    public Result update(AddAndUpdateCancleauditReason addAndUpdateUnqualiReason) {
        if(ToolsUtils.isEmpty(addAndUpdateUnqualiReason)){
            return Result.fail("入参不可为空");
        }
        List<LabMaininfoXsReason> unqualifiedReasons = addAndUpdateUnqualiReason.getCancleauditReasons();
        boolean save = this.updateBatchById(unqualifiedReasons);
        if(save){
            return Result.fail("修改成功");
        }else {
            return Result.fail("修改失败");
        }
    }

    @Override
    public Result dalete(DeleteParam deleteParam) {
        List<String> ids =  Arrays.asList(deleteParam.getIds().split(","));
        Boolean delete = this.removeByIds(ids);
        if(delete){
            return Result.fail("删除成功");
        }else {
            return Result.fail("删除失败");
        }
    }

    @Override
    public Result getCancleauditList(LabMaininfoXsReason labUnqualifiedReason) {
        MPJLambdaWrapper<LabMaininfoXsGroup> queryWrapper = new MPJLambdaWrapper<>();
        SysUser  sysUser =  RedisUserManager.getUser();
        if(ToolsUtils.isNotEmpty(sysUser)){
            queryWrapper.eq(LabMaininfoXsGroup::getOrgId,sysUser.getOrgId());
        }
        queryWrapper.eq(LabMaininfoXsGroup::getCancleauditCode,labUnqualifiedReason.getCancleauditCode());
        queryWrapper.eq(LabMaininfoXsGroup::getDelFlag,0);
        queryWrapper.orderByAsc(LabMaininfoXsGroup::getSeq);
        List<LabMaininfoXsGroup> labUnqualifiedGroups =  labMaininfoXsGroupMapper.selectList(queryWrapper);
        if(ToolsUtils.isNotEmpty(labUnqualifiedGroups)){
            for (LabMaininfoXsGroup labUnqualifiedGroup : labUnqualifiedGroups) {
                MPJLambdaWrapper<LabMaininfoXsReason> queryWrapperdel = new MPJLambdaWrapper<>();
                queryWrapperdel.eq(LabMaininfoXsReason::getDelFlag,0);
                queryWrapperdel.eq(LabMaininfoXsReason::getCancleauditGroupId,labUnqualifiedGroup.getId());
                if(ToolsUtils.isNotEmpty(sysUser)){
                    queryWrapperdel.eq(LabMaininfoXsReason::getOrgId,sysUser.getOrgId());
                }
                List list = labMaininfoXsReasonMapper.selectList(queryWrapperdel);
                labUnqualifiedGroup.setLabMaininfoXsReasons(list);
            }
        }
        return Result.succ(labUnqualifiedGroups);
    }
}
